// Created by WXX on 2021/11/30 15:22
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：4 ms, 在所有 C++ 提交中击败了85.06%的用户
 * 内存消耗：8.6 MB, 在所有 C++ 提交中击败了24.67%的用户
 */
class Solution {
public:
    int maxSumTwoNoOverlap(vector<int> &nums, int a, int b) {
        return max(work(nums, a, b), work(nums, b, a));
    }

    // 返回前一段长度为a，后一段长度为b的两段区间最大和
    int work(vector<int> &nums, int a, int b) {
        int n = nums.size();
        vector<int> s(n + 1);
        for (int i = 1; i <= n; i++) s[i] = s[i - 1] + nums[i - 1];

        int res = 0;
        for (int i = a + b, maxa = 0; i <= n; i++) {
            maxa = max(maxa, s[i - b] - s[i - b - a]);
            res = max(res, s[i] - s[i - b] + maxa);
        }
        return res;
    }
};

int main() {

    vector<int> nums = {0, 6, 5, 2, 2, 5, 1, 9, 4};
    cout << Solution().maxSumTwoNoOverlap(nums, 1, 2) << endl;  // 20

    return 0;
}
